/*
 * @Author: jhw
 * @Date: 2025-08-04 22:58:44
 * @LastEditors: jhw
 * @LastEditTime: 2025-08-22 17:52:24
 * @Description:
 */
import { fileURLToPath, URL } from 'node:url';

import { defineConfig, loadEnv } from 'vite';
import type { TerserOptions } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import vueDevTools from 'vite-plugin-vue-devtools';
import WindiCSS from 'vite-plugin-windicss';
import { visualizer } from 'rollup-plugin-visualizer';
import path from 'node:path';

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      vueJsx(),
      vueDevTools(),
      WindiCSS(),
      visualizer({
        open: false,
        filename: 'report.html',
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    build: {
      sourcemap: true,
      outDir: path.resolve(__dirname, './' + env.VITE_BUILD_NAME),
      rollupOptions: {
        output: {
          manualChunks: {
            vue: ['vue'],
            'vue-router': ['vue-router'],
            'element-plus': ['element-plus'],
          },
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
        },
      },
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      } as TerserOptions,
    },
    server: {
      proxy: {
        '/api': 'http://localhost:3001',
        '/uploads': 'http://localhost:3001',
        '/editorApi': {
          target: 'http://localhost:8001',
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/editorApi/, 'editor'),
        },
      },
    },
  };
});
